Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

493
Visualizações
Mongodb - unknown top level operator: $elemMatch"

I have a collection which contains some documents as follows,

{
  "transactionId": 3432423,
  "reviews": [
    {
      "fromUser": {
        "userId": "5236aa1acd6e"
      },
      "toUser": {
        "userId": "0ec8db9544cc"
      },
      "rating": 4.3,
      "comment": ""
    },
    {
    "toUser": {
        "userId": "5236aa1acd6e",
        "email": "yourlife@gmail.com",
        "firstName": "your",
        "lastName": "life"
      },
      "fromUser": {
        "userId": "0ec8db9544cc",
        "email": "mylife@gmail.com",
        "firstName": "my",
        "lastName": "life"
      },
      "rating": 4.3,
      "comment": ""
    }
  ]

}

i need to check if subdocument reviews present inside the document. i have used this query,

db.getCollection('_testCollection').find({  "$elemMatch": { "reviews": { "$exists": false } }})

it throws an error saying,

"errmsg" : "unknown top level operator: $elemMatch",
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Neither $elemMatch or $exists is what you want to use to determine if reviews is an empty array.

Instead, either compare against []:

db.test.find({reviews: []})

Or use the $size operator:

db.test.find({reviews: {$size: 0}})
over 4 years ago · Santiago Trujillo Relatório

0

From the Docs :-

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

db.getCollection('_testCollection').find({  "reviews": {"$elemMatch" : { "$exists": false } }})

$exists :- true or false // depend on requirement

Thanks

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda